package cn.bdqn.asecu.config;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import cn.bdqn.pojo.User;
import org.springframework.web.servlet.HandlerInterceptor;
import org.springframework.web.servlet.ModelAndView;

public class LoginInterceptor implements HandlerInterceptor{
	//执行handler方法之前执行
	public boolean preHandle(HttpServletRequest request,
			HttpServletResponse response, Object handler) throws Exception {

				 //判断session
				 HttpSession session  = request.getSession();
				//从session中取出用户身份信息
		         User user = (User) session.getAttribute("user");

				if(user != null){
					//身份存在，放行
					return true;
				}

//				//执行这里表示用户身份需要认证，跳转登陆页面
				response.sendRedirect("/");
				return false;
	}

	//进入handler方法之后，返回modelAndView执行
	public void postHandle(HttpServletRequest request,
			HttpServletResponse response, Object handler,
			ModelAndView modelAndView) throws Exception {

	}

	//执行handler完成执行此方法
	public void afterCompletion(HttpServletRequest request,
			HttpServletResponse response, Object handler, Exception ex)
			throws Exception {
	}

}
